package csu.web.mypetstore.web.servlet;

import csu.web.mypetstore.domain.Account;
import csu.web.mypetstore.service.AccountService;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.util.Objects;

public class EditAccountServlet extends HttpServlet {
    private static final String EDIT_ACCOUNT = "/WEB-INF/jsp/account/editAccountForm.jsp";
    public static final String MAIN = "/WEB-INF/jsp/catalog/main.jsp";
    private AccountService accountService;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession httpSession = req.getSession();
        Account account = (Account) httpSession.getAttribute("account");
        String password = req.getParameter("password");
        String repeatedPassword = req.getParameter("repeatedPassword");
        accountService = new AccountService();

        if (!Objects.equals(password, repeatedPassword)) {
            httpSession.setAttribute("message", "两次输入的密码不一致！");
            req.getRequestDispatcher(EDIT_ACCOUNT).forward(req, resp);
            return;
        }
        //如果提供了新密码，则更新密码
        if (!Objects.equals(password, "")) account.setPassword(password);

        //检查是否有任何必填字段为空
        if (Objects.equals(req.getParameter("account.firstName"), "") ||
                Objects.equals(req.getParameter("account.lastName"), "") ||
                Objects.equals(req.getParameter("account.email"), "") ||
                Objects.equals(req.getParameter("account.phone"), "") ||
                Objects.equals(req.getParameter("account.address1"), "") ||
                Objects.equals(req.getParameter("account.city"), "") ||
                Objects.equals(req.getParameter("account.state"), "") ||
                Objects.equals(req.getParameter("account.zip"), "") ||
                Objects.equals(req.getParameter("account.country"), "")) {
            httpSession.setAttribute("messageWarning", "除Address2外，其他信息不能为空");
            httpSession.removeAttribute("message");
            req.getRequestDispatcher(EDIT_ACCOUNT).forward(req, resp);
            return;
        }

        //更新账户字段
        account.setFirstName(req.getParameter("account.firstName"));
        account.setLastName(req.getParameter("account.lastName"));
        account.setEmail(req.getParameter("account.email"));
        account.setPhone(req.getParameter("account.phone"));
        account.setAddress1(req.getParameter("account.address1"));
        account.setAddress2(req.getParameter("account.address2"));
        account.setCity(req.getParameter("account.city"));
        account.setState(req.getParameter("account.state"));
        account.setZip(req.getParameter("account.zip"));
        account.setCountry(req.getParameter("account.country"));
        account.setLanguagePreference("account.languagePreference");
        account.setFavouriteCategoryId(req.getParameter("account.favouriteCategoryId"));
        account.setListOption(req.getParameterValues("account.listOption") != null);
        account.setBannerOption(req.getParameterValues("account.bannerOption") != null);

//        //初始化日志服务并更新日志
//        LogService logService = new LogService();
//        List<Log> logList = (List<Log>) httpSession.getAttribute("logList");
//
//        logService.updateLog(logList, "更新账户信息", account);
//        httpSession.setAttribute("logList", logList);
        //在数据库中更新账户
        accountService.updateAccount(account);
        //移除旧的会话属性并设置更新后的账户
        httpSession.removeAttribute("account");
        httpSession.removeAttribute("messageWarning");
        httpSession.removeAttribute("message");
        httpSession.setAttribute("account", account);
        //将请求转发回编辑账户表单
        req.getRequestDispatcher(EDIT_ACCOUNT).forward(req, resp);
    }
}
